package com.fenghongzhang.module_main.ui.search

import android.os.Bundle
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.fenghongzhang.lib_core.base.BaseToolbarActivity
import com.fenghongzhang.lib_core.base.observeWithLifecycle
import com.fenghongzhang.lib_core.cons.IntentData
import com.fenghongzhang.lib_core.utils.Router
import com.fenghongzhang.module_main.R
import com.fenghongzhang.module_main.databinding.ActivitySearchBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.Ui.SearchActivity)
class SearchActivity : BaseToolbarActivity<SearchViewModel, ActivitySearchBinding>(), View.OnClickListener {


    override val mViewModel: SearchViewModel by viewModels()

    override val mLayoutResId: Int
        get() = R.layout.activity_search


    private val mSearchSuggestionsFragment by lazy {
        SearchSuggestionsFragment.newInstance(mOnlyTopic)
    }
    override fun initListener() {
        mViewModel.mSearchHistoryData.observeWithLifecycle(this) {
            if (it.isEmpty())
                mBinding.lSearchHistory.visibility = View.GONE
            else {
                mBinding.lSearchHistory.visibility = View.VISIBLE
                updateSearchHistory(it)
            }
        }

        mBinding.etSearch.setOnEditorActionListener { _, code, _ ->
            if (code == EditorInfo.IME_ACTION_SEARCH) {
                //只显示话题时，不跳转
                if (mViewModel.mKeywords.value.isEmpty() || mOnlyTopic) {
                    return@setOnEditorActionListener true
                }
            //    SearchRetActivity.jump(mViewModel.mKeywords.value)
             //   hideSuggestion()
                //添加新的记录
                mViewModel.addSearchHistory(mViewModel.mKeywords.value)
                return@setOnEditorActionListener true
            }
            false
        }
    }

    //显示搜索记录
    private fun updateSearchHistory(list: List<String>) {
        mBinding.run {
            if (list.isEmpty()) {
                flowSearchHistory.visibility = View.GONE
                return
            }
            if (flowSearchHistory.visibility != View.VISIBLE)
                flowSearchHistory.visibility = View.VISIBLE
            flowSearchHistory.removeAllViews()
            list.forEach {
                val historyView = createFlowItem(HotListItem("", it, HotListType.HISTORY))
                flowSearchHistory.addView(historyView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                historyView.setOnClickListener(this@SearchActivity)
            }
        }
    }

    private fun createFlowItem(item: HotListItem, isTopic: Boolean = false): TextView {
        val textView = TextView(this)
        textView.includeFontPadding = true
        if (isTopic)
            textView.text = "#${item.name}#"
        else
            textView.text = item.name
        textView.maxWidth = 300
        textView.tag = item
        textView.maxLines = 1
        textView.ellipsize = TextUtils.TruncateAt.END
        textView.setBackgroundResource(R.drawable.shape_search_history_tab_4dp)
        return textView
    }

    @Autowired(name = IntentData.KEY_SEARCH_TYPE)
    lateinit var mSearchType: String

    private var mOnlyTopic = false

    //记录原始窗口高度
    private var mWindowHeight = 0

    override fun initView() {
        super.initView()
        showSoftKeyBoard(mBinding.etSearch)
        mOnlyTopic = mSearchType == "topic"
        supportFragmentManager.beginTransaction().add(R.id.l_container, mSearchSuggestionsFragment, SearchSuggestionsFragment.TAG)
            .hide(mSearchSuggestionsFragment)
            .commitAllowingStateLoss()
    }
    override fun onClick(v: View?) {

    }

    override fun initData() {
        mBinding.viewModel = mViewModel
        //获取搜索记录
        mViewModel.getSearchHistory()
        //获取热门搜索
       // mViewModel.getSearchHotData()
        //搜索建议
       // mViewModel.getSearchSuggestion()
    }

}